<?php
/**
 * Created by PhpStorm.
 * User: liu
 * Date: 2018/5/4
 * Time: 17:50
 */

namespace app\admin\controller\sunfc;


use app\admin\controller\Base;
use think\Db;

class Point extends Base
{
    /**
     * 获取积分日志表（GET）
     * URL：admin/sunfc.point/log
     * @return [type] [description]
     */
    public function log()
    {
        $log = Db::name('dd_users_balance_log');
        if (request()->isAjax()) {

            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 10;

            $res= $log->join("iw_dd_users","iw_dd_users.uid=iw_dd_users_balance_log.uid")
                ->field("iw_dd_users.nickname,iw_dd_users_balance_log.sign,
                iw_dd_users_balance_log.number,iw_dd_users_balance_log.type,iw_dd_users_balance_log.content,
                iw_dd_users_balance_log.create_time")
                ->page($page, $limit)
                ->order('iw_dd_users_balance_log.id','desc')
                ->select();
            //对数据格式进行转换
            foreach ($res as $index => $re) {

                $res[$index]['create_time'] = date('Y-m-d H:i:s', $re['create_time']);

                if($res[$index]['sign']==1){
                    $res[$index]['sign'] = "收入";
                }else{
                    $res[$index]['sign'] = "支出";
                }

                if($res[$index]['type']==1){
                    $res[$index]['type'] = "积分";
                }elseif($res[$index]['type']==2){
                    $res[$index]['type'] = "余额";
                }else{
                    $res[$index]['type'] = "微信";
                }
            }

            return json(['code' => 1, 'msg' => 'ok', 'count' => count($res), 'data' => $res]);
        }
        else {
            //数据总数
            $count = $log->count();
            $this->assign('count', $count);
            return $this->fetch();
        }
    }
    //日志搜索
    public function cases_search(){
        $log = Db::name('dd_users_balance_log');

        $page = input('page') ? input('page') : 1;
        $limit = input('limit') ? input('limit') : 10;

        $key = trim(input('user_id'));
        $map = [];
        if (!$key) {
            $this->error('没有内容');
        }else{
            $id = Db::name("dd_users")->field("uid")->where("nickname",$key)->find();
            if(isset($id["uid"])){
                $map["iw_dd_users_balance_log.uid"] =  $id["uid"];
            }
            else{
                $this->error('没有内容');
            }

        }

        if (input('sign')!=2) {
            $map["iw_dd_users_balance_log.sign"] = input('sign');
        }else{

        }

        if (input('type')!=0) {
            $map["iw_dd_users_balance_log.type"] = input('type');
        }else{

        }

        $res= $log->join("iw_dd_users","iw_dd_users.uid=iw_dd_users_balance_log.uid")
            ->field("iw_dd_users.nickname,iw_dd_users_balance_log.sign,
                iw_dd_users_balance_log.number,iw_dd_users_balance_log.type,iw_dd_users_balance_log.content,
                iw_dd_users_balance_log.create_time")
            ->where($map)
            ->page($page, $limit)
            ->order('iw_dd_users_balance_log.id','desc')

            ->select();
        foreach ($res as $index => $re) {
            $res[$index]['create_time'] = date('Y-m-d H:i:s', $re['create_time']);
            if($res[$index]['sign']==1){
                $res[$index]['sign'] = "收入";
            }else{
                $res[$index]['sign'] = "支出";
            }

            if($res[$index]['type']==1){
                $res[$index]['type'] = "积分";
            }elseif($res[$index]['type']==2){
                $res[$index]['type'] = "余额";
            }else{
                $res[$index]['type'] = "微信";
            }
        }
        $total = $log->page($page, $limit)
            ->where($map)
            ->count();
        return json(['code' => 1, 'msg' => 'ok', 'total' => $total, 'data' => $res]);
    }

    /**
     * 获取爱心公司表（GET）
     * URL：admin/sunfc.point/company
     * @return [type] [description]
     */
    public function company(){

        $company = Db::name('dd_company');
        if (request()->isAjax()) {

            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 10;

            $res= $company
                ->page($page, $limit)
                ->order('id','desc')
                ->select();

            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/60x60/quality/100/interlace/0";
            foreach ($res as $index => $re) {
                $res[$index]['logo'] = $qiniu_config['domain'] . '/' . $re['logo'] . $q_thumb;

            }
            return json(['code' => 1, 'msg' => 'ok', 'count' => count($res), 'data' => $res]);
        }
        else {
            //数据总数
            $count = $company->count();
            $this->assign('count', $count);
            return $this->fetch();
        }
    }
    //公司搜索
    public function company_search(){
        $log = Db::name('dd_company');

        $page = input('page') ? input('page') : 1;
        $limit = input('limit') ? input('limit') : 10;

        $map = [];
        if(trim(input('address'))!=""){
            $map['address'] = trim(input('address'));
        }
        if(trim(input('key'))!=""){
            $map['company|tel'] = trim(input('key'));
        }

        $res= $log->where($map)
            ->page($page, $limit)
            ->order('id','desc')
            ->select();

        $qiniu_config = cmf_get_option('qiniu');
        // 七牛样式处理
        $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/60x60/quality/100/interlace/0";
        foreach ($res as $index => $re) {
            $res[$index]['logo'] = $qiniu_config['domain'] . '/' . $re['logo'] . $q_thumb;

        }
        return json(['code' => 1, 'msg' => 'ok', 'total' => count($res), 'data' => $res]);
    }

    //添加新公司信息
    public function add_company()
    {
        if (request()->isAjax()) {
            $post = input('');
            $data = $post['data'];
            unset($data['file']);
            // dump($data);
            $data_id = Db::name('dd_company')->insertGetId($data);
            if ($data_id) {
                return json(['code' => 1, 'msg' => '添加成功']);
            }
            else{
                return json(['code' => 1, 'msg' => '添加失败']);
            }

        } else {
            $data_city = Db::name('dd_open_city')
                ->field("city_name")
                ->where("is_show",1)
                ->order("id","asc")
                ->select();
            $this->assign("city",$data_city);
            return $this->fetch();
        }
    }
    //编辑公司信息
    public function edit_company()
    {
        if (request()->isAjax()) {
            $data = input();
            unset($data['file']);
            $res = Db::name('dd_company')->where('id', $data['id'])->update($data);
            if ($res === false) {
                $this->success('操作失败');
            }
            $this->success('操作成功');

        } else {
            $id = input('id');
            if (!$id) {
                $this->error('id 异常');
            }
            $res = Db::name('dd_company')->find($id);
            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/120x120/quality/100/interlace/0";
            $res['logo2'] = $qiniu_config['domain'] . '/' . $res['logo'] . $q_thumb;
            $this->assign('item', $res);
            $data_city = Db::name('dd_open_city')
                ->field("city_name")
                ->where("is_show",1)
                ->order("id","asc")
                ->select();
            $this->assign("city",$data_city);
            return $this->fetch();
        }
    }

    //设置是否展示
    public function set_show()
    {
        $data_id = input('data_id');
        if(Db::name('dd_company')->where('id', $data_id)->update(['is_show' => input('is_show')])){
            return json(['code' => 1, 'msg' => '操作成功']);
        }else{
            return json(['code' => 0, 'msg' => '操作失败']);
        }
    }


}